<template>
  <div class="app-container calendar-list-container">
    <div v-if="!opShow">
      <search :list="queryList" :queryEntity="queryEntity"></search>
      <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="订单号" :permissions="permissions" @add="doAdd"></buttons>
      <grid :columns="columns" :multi="false" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="125" @change="rowCilck" :queryEntity="queryEntity" :pagination="true"
        @back="doBack" @out="doOut" :permissions="permissions" @edit="doEdit" @view="doView" @pass="doPass" @cancel="doCancel" @delete="doDelete" @finish="doFinish" @open="doOpen">
      </grid>
    </div>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import { Message, MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
import { YesOrNoFormatter, SaleOrderStatusFormatter } from '@/const/dict'
import search from '@/components/Search'
import { colunms } from '@/const/colunms'
import { dict } from '@/const/dict'
import { queryField } from '@/const/query'
import { bgColor } from '@/const/bgColor'


export default {
  components: { grid, buttons, search },
  data () {
    return {
      queryUrl: url.SALE_LIST_URL,
      disabled: true,
      opShow: false,
      queryEntity: { includeStatus: ["CHECKED", "UN_CHECK", "FINISH"] },
      columns: [
        {
          label: '操作', width: 200, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '退货', clickName: 'back', auth: 'sale_back', valueProp: "orderStatus", showValue: ["FINISH", "CHECKED"] },
            { name: '出库', clickName: 'out', auth: 'sale_out', valueProp: "orderStatus", showValue: ["CHECKED", "FINISH"] },
            { name: '编辑', clickName: 'edit', auth: 'sale_edit', valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '查看', clickName: 'view', auth: 'sale_view' },
            { name: '删除', clickName: 'delete', auth: 'sale_delete', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '通过', clickName: 'pass', auth: 'sale_pass', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '取消', clickName: 'cancel', auth: 'sale_cancel', confirm: true, valueProp: "orderStatus", showValue: ["CHECKED", "FINISH"] },
            { name: '开启', clickName: 'open', auth: 'sale_open', confirm: true, valueProp: "orderStatus", showValue: ["FINISH"] },
            { name: '完成', clickName: 'finish', auth: 'sale_finish', confirm: true, valueProp: "orderStatus", showValue: ["CHECKED"] }
          ]
        },
        { label: '订单号', width: 180, prop: 'orderNo', align: 'left' },
        { label: '仓库名称', width: 120, prop: 'warehouseName', align: 'left' },
        { label: '客户名称', width: 200, prop: 'customerName', align: 'left' },
        { label: '数量', width: 80, prop: 'quantity', align: 'center' },
        { label: '卷数', width: 80, prop: 'total', align: 'center' },
        { label: '币种', width: 80, prop: 'currency', align: 'center' },
        { label: '总价', width: 80, prop: 'totalAmount', align: 'center' },
        { label: '订单状态', width: 80, prop: 'orderStatus', align: 'center', formatter: SaleOrderStatusFormatter, template: 'bgColor', bgColor: bgColor().SaleOrderStatus },
        { label: '是否对账', width: 80, prop: 'billStatus', align: 'center', formatter: YesOrNoFormatter, template: 'bgColor', bgColor: bgColor().YesOrNo },
        { label: '发货时间', width: 100, prop: 'sendTime', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }

      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'sale_add', ctr: false }
        //    { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'sale_edit', ctr: true },
        //    { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'sale_view', ctr: true },
        //   { name: '删除', type: 'primary', icon: 'edit', click: 'delete', auth: 'sale_delete', ctr: true, confirm: true },
        //   { name: '通过', type: 'primary', icon: 'edit', click: 'pass', auth: 'sale_pass', ctr: true, confirm: true },
        //    { name: '取消', type: 'primary', icon: 'edit', click: 'cancel', auth: 'sale_cancel', ctr: true, confirm: true },
        //   { name: '完成', type: 'primary', icon: 'edit', click: 'finish', auth: 'sale_finish', ctr: true, confirm: true }
      ],
      queryList: [
        { name: '订单号', type: 'text', prop: 'orderNoLike', placeholder: "订单号" },
        { name: '创建时间', type: 'date', prop: 'startTime', placeholder: "" },
        { name: '----', type: 'date', prop: 'endTime', placeholder: "", },
        { name: '订单状态', type: 'select', prop: 'orderStatus', placeholder: "订单状态", values: dict().SaleOrderStatus, label: 'name', value: 'value' },
        { name: '客户名称', type: 'query', prop: 'customerCode', rowValue: 'customerCode', rowName: 'customerName', url: url.CUSTOMER_LIST_URL, columns: colunms().customerColumns, pagination: true, queryList: queryField().customer, queryEntity: { customerType: 'CUSTOMER' } }
      ]
    }
  },
  created () { },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
    },
    doOut (row) {
      this.$router.push({ path: '/prod/sale/out', query: { orderNo: row.orderNo, customerName: row.customerName } })
    },
    doBack (row) {
      this.$router.push({ path: '/prod/sale/back', query: { orderNo: row.orderNo, customerName: row.customerName } })
    },
    doAdd () {
      this.$router.push({ path: '/prod/sale/add', query: {} })
    },
    doView (row) {
      //const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({
          path: '/prod/sale/view',
          query: { orderNo: row.orderNo }
        })
      }
    },
    doEdit (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({
          path: '/prod/sale/edit',
          query: { orderNo: row.orderNo }
        })
      }
    },
    doPass (row) {
      //const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.passOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doFinish (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.finishOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doOpen (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.openOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    deleteOp (orderNo) {
      return request({
        url: url.SALE_DELETE_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    doDelete (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.deleteOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    openOp (orderNo) {
      return request({
        url: url.SALE_OPEN_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    finishOp (orderNo) {
      return request({
        url: url.SALE_FINISH_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    passOp (orderNo) {
      return request({
        url: url.SALE_PASS_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    cancelOp (orderNo) {
      return request({
        url: url.SALE_CANCEL_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    doCancel (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.cancelOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
